import {conpanyUrl, webStocketUrl} from '/public/static';
export  function ajax(url,data,type='GET') {
          return new Promise((resolve,reject) => {
            my.request({
              url: conpanyUrl+url,
              method: type,
              data,
              dataType: 'json',
              success: function(res) {
                console.log('res---->',res)
                if(typeof res.data === "string") {
                    console.log("success，，，data为sting,token过期,,,,,,,")
                  // 判断token是否过期，过期则跳到登录页
                  let strIndex = res.data.indexOf('{'); //{code: 10040, msg: "token过期"}
                  let tokenStr = JSON.parse(res.data.slice(strIndex)) ;
                  console.log(tokenStr)
                  if(tokenStr.msg === 'token过期') {
                    console.log("token过期,,,,,,,")
                    my.reLaunch({url: '/pages/login/login'})
                  }
                }
                resolve(res)
              },
              fail: function(res) {
                console.log(res)
                my.hideLoading();
                if(typeof res.data === "string") {
                    console.log("fail，，，data为sting,token过期,,,,,,,")
                  // 判断token是否过期，过期则跳到登录页
                  let strIndex = res.data.indexOf('{'); //{code: 10040, msg: "token过期"}
                  let tokenStr = JSON.parse(res.data.slice(strIndex)) ;
                  if(tokenStr.msg === 'token过期') {
                    console.log("token过期,,,,,,,")
                    my.reLaunch({url: '/pages/login/login'})
                  }
                }else{
                   my.alert({content: res.errorMessage});
                }
                reject(res)
               
              },
              // complete: function(res) {
              //   my.hideLoading();
              //   my.alert({content: 'complete'});
              // }
            });
          })
      }
export  function ajax2(url,data,type='GET') {
          return new Promise((resolve,reject) => {
            my.request({
              url: webStocketUrl+url,
              method: type,
              data,
              dataType: 'json',
              success: function(res) {
                resolve(res)
                // my.alert({content: 'success'});
              },
              fail: function(res) {
                reject(res)
                console.log(res)
                my.alert({content: res.errorMessage});
              },
              // complete: function(res) {
              //   my.hideLoading();
              //   my.alert({content: 'complete'});
              // }
            });
          })
      }


